package com.edz.dudumifi.utils;

import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * author: Created by EDZ on 2018/5/17.
 * function:HttpPost请求
 */

public class HttpUtils {
    static HttpURLConnection connection = null;
    static BufferedReader reader = null;
    private static final String TAG = "HttpUtils";

    public static String sendRequest(String url){
        String req_response = null;
        try{
            URL url1 = new URL(url);
            connection = (HttpURLConnection)url1.openConnection();
            connection.setRequestMethod("POST");
            connection.setConnectTimeout(8000);
            connection.setReadTimeout(8000);
            InputStream inputStream = connection.getInputStream();

            //对获取到的输入流进行读取
            reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null){
                response.append(line);
            }
            req_response = response.toString();
        }catch (Exception e){
            Log.e(TAG,"[ HttpURLConnection ] 异常" + e.getMessage());
        }finally {
            if(reader != null){
                try{
                    reader.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }

            if(connection != null){
                connection.disconnect();
            }
        }
        return req_response;
    }
}
